import { createRouter, createWebHashHistory } from "vue-router";
import { getToken } from "@/utils/auth.js";
import { toast } from "@/utils/index.js";
import { useAdminStore } from "@/stores/admin";
import pinia from "@/stores"
import nProgress from "nprogress";
import "nprogress/nprogress.css";
const store = useAdminStore(pinia);
const routes = [
  {
    path: "/",
    name:"admin",
    // redirect:"/home",
    // meta:{
    //   title:"首页",
    // },
    component: () => import("../views/layout/Layout.vue"),
    children:[
    //   {
    //   path:'/',
      
    //   component:()=>import("../views/HomeView.vue")
    // }
  ]
  },
  {
    path: "/login",
    meta:{
      title:"登录页",
    },
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/404View.vue"),
  },
];
const asyncRoutes = [
  {
    path: "/",
    name: "/",
    meta: {
      title: "首页-主控台",
    },
    component: () => import("../views/menus/Home.vue"),
  },
  {
    path: "/goods/list",
    name: "/goods/list",
    meta: {
      title: "商品管理",
    },
    component: () => import("../views/menus/GoodsList.vue"),
  },
  {
    path: "/category/list",
    name: "/category/list",
    meta: {
      title: "分类管理",
    },
    component: () => import("../views/element/Table.vue"),
  },
  {
    path: "/skus/list",
    name: "/skus/list",
    meta: {
      title: "规格管理",
    },
    component: () => import("../views/menus/SkusList.vue"),
  },
  {
    path: "/coupon/list",
    name: "/coupon/list",
    meta: {
      title: "优惠券管理",
    },
    component: () => import("../views/menus/CouponList.vue"),
  },
  {
    path: "/user/list",
    name: "/user/list",
    meta: {
      title: "用户管理",
    },
    component: () => import("../views/menus/UserList.vue"),
  },
  {
    path: "/level/list",
    name: "/level/list",
    meta: {
      title: "会员等级",
    },
    component: () => import("../views/menus/LevelList.vue"),
  },
  {
    path: "/order/list",
    name: "/order/list",
    meta: {
      title: "订单管理",
    },
    component: () => import("../views/menus/OrderList.vue"),
  },
  {
    path: "/comment/list",
    name: "/comment/list",
    meta: {
      title: "评论管理",
    },
    component: () => import("../views/menus/CommentList.vue"),
  },
  {
    path: "/manager/list",
    name: "/manager/list",
    meta: {
      title: "管理员管理",
    },
    component: () => import("../views/menus/ManagerList.vue"),
  },
  {
    path: "/access/list",
    name: "/access/list",
    meta: {
      title: "权限管理",
    },
    component: () => import("../views/menus/AccessList.vue"),
  },
  {
    path: "/role/list",
    name: "/role/list",
    meta: {
      title: "角色管理",
    },
    component: () => import("../views/menus/RoleList.vue"),
  },
  {
    path: "/setting/base",
    name: "/setting/base",
    meta: {
      title: "基础设置",
    },
    component: () => import("../views/menus/SettingBase.vue"),
  },
  {
    path: "/setting/buy",
    name: "/setting/buy",
    meta: {
      title: "交易设置",
    },
    component: () => import("../views/menus/SettingBuy.vue"),
  },
  {
    path: "/setting/ship",
    name: "/setting/ship",
    meta: {
      title: "物流设置",
    },
    component: () => import("../views/menus/SettingShip.vue"),
  },
  {
    path: "/distribution/index",
    name: "/distribution/index",
    meta: {
      title: "分销员管理",
    },
    component: () => import("../views/menus/DistributionIndex.vue"),
  },
  {
    path: "/distribution/setting",
    name: "/distribution/setting",
    meta: {
      title: "分销设置",
    },
    component: () => import("../views/menus/DistributionSetting.vue"),
  },
  {
    path: "/image/list",
    name: "/image/list",
    meta: {
      title: "图库管理",
    },
    component: () => import("../views/menus/ImageList.vue"),
  },
  {
    path: "/notice/list",
    name: "/notice/list",
    meta: {
      title: "公告管理",
    },
    component: () => import("../views/menus/NoticeList.vue"),
  },
];

function generateAsyncRoutes(menus){
  let hasNewRouter = false;
  let isGetAdminInfo = false;
  function mapRoute(menus){
    menus.forEach((menusItem) => {
      const isHasRouter = asyncRoutes.find((asyncRoute)=>{
        return asyncRoute.path === menusItem.frontpath;
      });
      if(isHasRouter && !router.hasRoute(menusItem.frontpath)){
        console.log(isHasRouter);
        router.addRoute("admin",isHasRouter);
        hasNewRouter = true;
        isGetAdminInfo = true;
      };
      if(menusItem.child && menusItem.child.length > 0){
        mapRoute(menusItem.child);
      }
    });
    console.log(router.getRoutes());
  }
  mapRoute(menus);
  return hasNewRouter;
}

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  nProgress.start();
  if(to.meta){
    document.title = to.meta.title + '-千锋后台管理系统';
  }
  const token = getToken();
  if (!token && to.path !== "/login") {
    toast("请先登录！", "error");
    return next("/login");
  }
  if (token && to.path == "/login") {
    toast("您已经登录，请勿重复登录！", "error");
    return next({
      path: from.path ? from.path : "/",
    });
  }
  let hasNewRouter = false;
  let isGetAdminInfo = false;
  if(token && !isGetAdminInfo){
    isGetAdminInfo = true;
    await store.getAdminInfo();
    hasNewRouter = generateAsyncRoutes(store.adminInfo.menus);
  }
  // if(token){
  //   await store.getAdminInfo();
  //   hasNewRouter = generateAsyncRoutes(store.adminInfo.menus);
  // }
  // next();
  hasNewRouter ? next(to.fullPath) : next();
});
router.afterEach((to,from)=>{
  nProgress.done();
});
export default router;
